package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author ItZheng
 * @Date 2023/6/24 20:12
 * @Version 1.5
 */

@Component
public class BeahaviorLike {

    @Autowired
    private ApArticleMapper apArticleMapper;


    @KafkaListener(topics = "ARTICLE_LIKE_TOPOC")
    public void onMessage(String message) {
        //校验参数：
        if (StringUtils.isNotEmpty(message)) {


            //将数据反序列化成 Map对象
            Map map = JSON.parseObject(message, Map.class);

            //调用Service修改保存数据
            Long articleId = (Long) map.get("articleId");
            int score = (int) map.get("likeScore");
            ApArticle apArticle = apArticleMapper.selectById(articleId);
            if (apArticle.getLikes() == null) {
                apArticle.setLikes(0);
            }
            apArticle.setLikes(apArticle.getLikes() + score);

            apArticleMapper.updateById(apArticle);
        }
    }

}
